package cn.edu.fzu.homemaking.service.impl;

import cn.edu.fzu.homemaking.api.OrderApi;
import cn.edu.fzu.homemaking.api.SsoApi;
import cn.edu.fzu.homemaking.api.WalletApi;
import cn.edu.fzu.homemaking.cache.LabelCache;
import cn.edu.fzu.homemaking.domain.entity.UserDemand;
import cn.edu.fzu.homemaking.domain.entity.UserWallet;
import cn.edu.fzu.homemaking.domain.enums.DemandStatusEnum;
import cn.edu.fzu.homemaking.domain.vo.UserDemandVo;
import cn.edu.fzu.homemaking.exception.ServiceException;
import cn.edu.fzu.homemaking.mapper.DemandMapper;
import cn.edu.fzu.homemaking.service.DemandService;
import cn.edu.fzu.homemaking.util.ResultUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author jiebin
 * @date 2023/4/1
 */

@Service("demandService")
public class DemandServiceImpl implements DemandService {

    @Resource
    private DemandMapper demandMapper;

    @Resource
    private OrderApi orderApi;

    @Resource
    private WalletApi walletApi;

    @Resource
    private LabelCache   labelCache;


    @Override
    public void insert(Long userId, Long labelId, String demand, String time, String address, Integer person,
                       BigDecimal price, String name, String phone, String email, DemandStatusEnum status) {
        List<UserDemand> list = demandMapper.list(userId,null,null,null,null,DemandStatusEnum.WAIT);
        BigDecimal total = price;
        for (UserDemand userDemand: list) {
            total = total.add(userDemand.getPrice());
        }
        total = total.add(new BigDecimal(orderApi.getUnPayMoney().getValue().toString()));
        UserWallet userWallet = ResultUtil.parseValue(walletApi.getWallet(), UserWallet.class);
        if (total.compareTo(userWallet.getSurplus())==1){
            throw new ServiceException("用户钱包余额不支持发布新的需求，请及时充值！");
        }
        demandMapper.insert(userId, labelId, demand, time, address, person, price, name, phone, email, status);
    }


    @Override
    public List<UserDemandVo> list(Long userId, Long labelId, String startTime, String endTime, String address,
                                   DemandStatusEnum status) {
        if (userId == null && !Objects.equals(DemandStatusEnum.WAIT, status)) {
            throw new ServiceException("公司只能查询待接单的需求");
        }
        List<UserDemand> result = demandMapper.list(userId, labelId, startTime, endTime, address, status);
        List<UserDemandVo> list = new ArrayList<>();
        for (UserDemand userDemand : result) {
            UserDemandVo userDemandVo = new UserDemandVo();
            BeanUtils.copyProperties(userDemand, userDemandVo);
            userDemandVo.setLabelName(labelCache.getLabelName(userDemand.getLabelId()));
            list.add(userDemandVo);
        }
        return list;
    }


    @Override
    public void updateDemandStatus(Long id, DemandStatusEnum status) {
        demandMapper.updateDemandStatus(id, status);
    }


    @Override
    public void delete(Long id) {
        Integer result = demandMapper.delete(id);
        if (result == 0) {
            throw new ServiceException("需求删除失败");
        }
    }


    @Override
    public UserDemand selectById(Long id) {
        return demandMapper.selectById(id);
    }

}
